package crazyboyfeng.autodroid.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import crazyboyfeng.autodroid.R;
import crazyboyfeng.autodroid.bean.TaskBean;
import crazyboyfeng.autodroid.tasks.TaskManager;
import crazyboyfeng.autodroid.utils.WeekTools;

public class TaskAdapter extends BaseAdapter{

	private TaskManager mTM = new TaskManager();
	private Context context;
	private ArrayList<TaskBean> taskList;
	public TaskAdapter(Context context,ArrayList<TaskBean> list){
		this.context=context;
		this.taskList=list;
	}
	@Override
	public int getCount() {
		return taskList.size();
	}

	@Override
	public TaskBean getItem(int i) {
		return taskList.get(i);
	}

	@Override
	public long getItemId(int i) {
		return taskList.get(i).hashCode();
	}

	@Override
	public View getView(int i, View convertView, ViewGroup parent) {
		ViewHolder holder = null;

		LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		 if (convertView == null) {
	            convertView = mInflater.inflate(R.layout.main_list, null);
	            holder = new ViewHolder();
	            holder.txtTitle = (TextView) convertView.findViewById(R.id.list_title);
	            holder.txtTime = (TextView) convertView.findViewById(R.id.list_time);
	            holder.txtTask = (TextView) convertView.findViewById(R.id.list_task);
	            holder.txtType = (TextView) convertView.findViewById(R.id.list_type);
	            holder.txtState = (TextView) convertView.findViewById(R.id.list_state);
	            holder.oneSwitch = (Switch) convertView.findViewById(R.id.list_switch);
	            holder.oneSwitch.setTag(i);
	            convertView.setTag(holder);
	        }
	        else {
	            holder = (ViewHolder) convertView.getTag();
	        }
		    TaskBean bean = (TaskBean)taskList.get(i);
	        holder.txtTitle.setText(bean.getName());
	        holder.txtTime.setText(bean.getTime());
	        holder.txtTask.setText(bean.getTask());
	        if(bean.getState().equals("Y")) holder.txtState.setText("开启");
	        else  holder.txtState.setText("关闭");
	        
	        holder.txtType.setText(WeekTools.getWeekMsg(bean.getType().split(" ")));
	        holder.oneSwitch.setChecked(bean.isActive());
	        holder.oneSwitch.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
	                int point = Integer.parseInt(v.getTag()+"");
	                TaskBean bean = getItem(point);
	                Switch sw = (Switch)v;
	                
	                //判断task是否是新建状态
	                if(bean.getTask().equals("") || bean.getTime().equals("")){
	                	Toast.makeText(context, "请设置任务详情！", Toast.LENGTH_LONG).show();
	                	sw.setChecked(false);
	                	return ;
	                }
	                
	            	//判断task是否是活动状态，不是为更新活动状态
	            	if(bean.isActive()!=sw.isChecked()){
	            		mTM.editTaskActive(bean.getId(), sw.isChecked());
	            	}
	            	mTM.setTaskActive(bean.getId(), sw.isChecked()); 
	            	if(sw.isChecked()){
	            		Toast.makeText(context, "任务:"+bean.getName()+" 已开启！", Toast.LENGTH_LONG).show();
	            	}else{
	            		Toast.makeText(context, "任务:"+bean.getName()+" 已关闭！", Toast.LENGTH_LONG).show();
	            	}
	            }
	        });
	        return convertView;
	}
	
    /*private view holder class*/
    private class ViewHolder {
        TextView txtTitle;
        Switch oneSwitch;
        TextView txtTime;
        TextView txtTask;
        TextView txtState;
        TextView txtType;
    }

}
